﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.section;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;

namespace Pixysoft.Weblications.Sango.Command.pvp_resource
{
    partial class SangoPvpResourceHandler
    {
        public pvpresource_list pvpresource_list()
        {
            string userCode = SangoManager.Authentication();

            pvpresource_list response = ReflectionManager.CreatePojo<pvpresource_list>();
            response.Succeed = false;

            Entities.Usr_Section section = UsrSectionDao.GetSection(userCode);

            List<Entities.Usr_Resource> list = UsrResourceDao.GetResources(section.Sectionserialcode, section.Scaletypecode);
            List<Entities.Cti_Resource> citylist = CtiResourceHelper.searchByLevel(section.Scaletypecode);

            Entities.Usr_Partymember member = PartyDao.tryGetActiveMember(userCode);

            // balance here.

            response.resources = GetResources(citylist, list, userCode);
            if (member != null)
            {
                Entities.Usr_Partymission farmMission = PartyDao.tryGetMission(member.Partyprimarycode, PtyMissionTypecodeCollection.FARMLAND);
                if (farmMission != null)
                {
                    foreach (section_resource body in response.resources)
                    {
                        body.addproductivity = (long)Math.Round(body.productivity * (1 + PartyFormular.GetResourceAddProductivityFactor(farmMission.Missionlevel)));

                    }
                }
            }
            response.myresources = GetUserResources(list, userCode);
            response.Succeed = true;
            return response;
        }

        private List<section_resource> GetResources(
            List<Entities.Cti_Resource> syss, List<Entities.Usr_Resource> orms, string usercode)
        {
            List<section_resource> list = new List<section_resource>();

            foreach (Entities.Cti_Resource sys in syss)
            {
                Entities.Usr_Resource orm = TryGetUserResource(orms, sys.Resourceserialcode);

                if (orm != null)
                {
                    if (!orm.Usercode.Equals(usercode, StringComparison.OrdinalIgnoreCase))
                        list.Add(UsrSectionFacade.ToResource(orm));
                }
                else
                {
                    list.Add(UsrSectionFacade.ToResource(sys));
                }
            }

            return list;
        }

        private Entities.Usr_Resource TryGetUserResource(List<Entities.Usr_Resource> orms, long serialcode)
        {
            foreach (Entities.Usr_Resource orm in orms)
            {
                if (orm.Resourceserialcode == serialcode)
                    return orm;
            }
            return null;
        }

        private List<section_resource> GetUserResources(List<Entities.Usr_Resource> orms, string usercode)
        {
            List<section_resource> list = new List<section_resource>();
            foreach (Entities.Usr_Resource orm in orms)
            {
                if (!orm.Usercode.Equals(usercode, StringComparison.OrdinalIgnoreCase))
                    continue;

                list.Add(UsrSectionFacade.ToResource(orm));
            }
            return list;
        }
    }
    public interface pvpresource_list : ISangoResponse
    {
        List<section_resource> resources { get;set;}

        List<section_resource> myresources { get;set;}
    }
}
